//
//  HMMainViewController.swift
//  微博
//
//  Created by 郭鉴增 on 2016/12/1.
//  Copyright © 2016年 郭鉴增. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加姿势图控制器
        
        //设置自定义的tabbar
        let hmTabBar = HMTabbar()
        
        //设置代理对象
        hmTabBar.HMDelegate = self
        
        //设置闭包
        hmTabBar.callBack = { [weak self] in
            print(self)
            //发微博的相关操作
            
        }
        
//        使用kvc方式给只读属性赋值
        setValue(hmTabBar, forKey: "tabBar")
        
        
        
        addChildViewController(childController: HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(childController: HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(childController: HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(childController: HMProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
        
    }
    
    //重载添加子视图控制器
    func addChildViewController(childController: UIViewController,imageName: String,title: String) {
        //设置tabbar图片
        childController.tabBarItem.image = UIImage(named:imageName)
        //修改图片渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //修改tabbar 文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        
        //设置tabbar的文字
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //合成一句
        childController.title = title 
        
        //创建导航视图控制器
        let nav = HMNavigationViewController(rootViewController: childController)
        
        //添加子视图控制器
        addChildViewController(nav)
        
        
        
        
    }
}
//MARK: - 遵守HMTabBarDelegate
extension HMMainViewController: HMTabBarDelegate{
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}





